
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using Red.Controls;
using Red.Controls.Shell;
using Red.Controls.ViewModels;
using Red.EngineData.View;

namespace Red.Toolkit.PhysicalDecorationEditor.ViewModels
{

    public class StructureViewItem : TreeItemEngine
    {
        #region C-tor

        public StructureViewItem( TreeItemEngine parent, string title, string toolTip, DataView view, Canvas image )
            : base( parent, title, toolTip, view, image )
        {
        }

        #endregion C-tor

        #region Interface

        #endregion Interface

        #region TreeItemEngine implementation

        protected override RedTreeItem CreateTreeItem( DataView childView )
        {
            return new StructureViewItem( this, childView.InteropName, "", childView, Image );
        }

        protected override void OnSynchronized()
        {
            Title = String.Format( "{0}: {1}", View.InteropName, View.Value );
        }
  
        #endregion TreeItemEngine implementation
    }

    public class StructureViewPhysicsActorItem : TreeItemEngine
    {
        private static Canvas _colliderListIcon = RedApplication.FindCanvasResource( "appbar_base_select" );

        public StructureViewPhysicsActorItem( StructureViewPhysicsActorListItem parent, string title, string tooltip, DataView view, Canvas image )
            : base( parent, title, tooltip, view, image )
        {
        }

        public override IEnumerable<CommandWrapper> Commands
        {
            get { return new List<CommandWrapper> { new CommandWrapper( StructureViewCommands.RemoveActor ) }; }
        }
        
        protected override RedTreeItem CreateTreeItem( DataView childView )
        {
            if ( childView.InteropName.Contains( "shapes" ) )
            {
                return new StructureViewPhysicsColliderListItem( this, "Collider", "Collider", childView, _colliderListIcon );
            }
            return null;
        }

        protected override void OnSynchronized()
        {
            AttachedView = View.GetSubView( "data" );
        }
    }

    public class StructureViewPhysicsActorListItem : TreeItemEngine
    {
        public StructureViewPhysicsActorListItem( TreeItemEngine parent, string title, string tooltip, DataView view, Canvas image )
            : base( parent, title, tooltip, view, image )
        {
        }

        public override IEnumerable<CommandWrapper> Commands
        {
            get { return new List<CommandWrapper> { new CommandWrapper( StructureViewCommands.AddActor ) }; }
        }
        
        protected override RedTreeItem CreateTreeItem( DataView childView )
        {
            return new StructureViewPhysicsActorItem( this, Title + childView.InteropName, ToolTip, childView, Image );            
        }

        protected override void OnSynchronized()
        {
            IsExpanded = true;
        }
    }

    public class StructureViewPhysicsColliderItem : TreeItemEngine
    {
        public StructureViewPhysicsColliderItem( TreeItemEngine parent, string title, string tooltip, DataView view, Canvas image )
            : base( parent, title, tooltip, view, image, null, String.Empty, false )
        {
        }

        public override IEnumerable<CommandWrapper> Commands
        {
            get { return new List<CommandWrapper> { new CommandWrapper( StructureViewCommands.RemoveCollider ) }; }
        }

        protected override void OnSynchronized()
        {
            AttachedView = View.GetSubView( "data" );
        }
    }

    public class StructureViewPhysicsColliderListItem : TreeItemEngine
    {
        public StructureViewPhysicsColliderListItem( TreeItemEngine parent, string title, string tooltip, DataView view, Canvas image )
            : base( parent, title, tooltip, view, image )
        {
        }

        public override IEnumerable<CommandWrapper> Commands
        {
            get { return new List<CommandWrapper> { new CommandWrapper( StructureViewCommands.AddCollider ) }; }
        }

        protected override RedTreeItem CreateTreeItem( DataView childView )
        {
            return new StructureViewPhysicsColliderItem( this, Title + childView.InteropName, ToolTip, childView, Image );
        }
    }

    public class StructureViewItemRoot : TreeItemEngine
    {
        private static Canvas _actorListIcon = RedApplication.FindCanvasResource( "appbar_layer" );

        #region Properties

        #endregion Properties

        #region C-tor

        public StructureViewItemRoot( DataView rootView )
            : base( null, "Structure", "Structure", rootView, null, null, "physicsDecoration" )
        {        
        }

        #endregion C-tor

        #region TreeItemEngine implementation

        protected override RedTreeItem CreateTreeItem( DataView childView )
        {
            if ( childView.InteropName == "Actors" )
            {
                return new StructureViewPhysicsActorListItem( this, "Actors", "Actors array", childView, _actorListIcon );
            }
            return null;
        }

        #endregion TreeItemEngine implementation
    }
}
